import { createRouter, createWebHashHistory } from 'vue-router'
import useUserStore from '@/store/user/user'
// 一级路由
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/login',
      component: () => import('../views/login/login.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/main.vue')
    },
    {
      path: '/:pathMatch(.*)',
      component: () => import('../views/not-found/NotFound.vue')
    }
  ]
})

// 二级路由
const localRoutes: any[] = [
  {
    path: '/main/index',
    component: () => import('@/views/main/index/index.vue')
  },
  {
    path: '/main/tasksquare',
    component: () => import('@/views/main/tasksquare/tasksquare.vue')
  },
  {
    path: '/main/projects',
    name: 'projects',
    component: () => import('@/views/main/projects/projects.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/tasks',
    name: 'tasks',
    component: () => import('@/views/main/tasks/tasks.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/taskdetail/:no',
    name: 'taskdetail',
    component: () => import('../views/main/taskdetail/taskdetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/users/:no',
    component: () => import('@/views/main/users/users.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/createproject',
    component: () => import('@/views/main/createproject/createproject.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/createtask/:no',
    component: () => import('@/views/main/publishtask/publishtask.vue'),
    meta: { requiresAuth: true }
  }
]
localRoutes.forEach((item) => router.addRoute('main', item))

// projects模块的三级路由
const projectsRoutes = [
  {
    path: '/main/projects/implementation',
    component: () => import('@/views/main/projects/c-cpns/projects-implementation.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/projects/finished',
    component: () => import('@/views/main/projects/c-cpns/projects-finished.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/projects/paused',
    component: () => import('@/views/main/projects/c-cpns/projects-paused.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/main/projects/details/:no',
    name: 'projects-details',
    component: () => import('@/views/main/projects/c-cpns/projects-item-details.vue'),
    meta: { requiresAuth: true }
  }
]
projectsRoutes.forEach((item) => router.addRoute('projects', item))

// tasks模块的三级路由
const tasksRoutes = [
  {
    path: '/main/tasks/review',
    component: () => import('@/views/main/tasks/c-cpns/task-review.vue'),
    meta: { requiresAuth: true }
  }
]
tasksRoutes.forEach((item) => router.addRoute('tasks', item))

router.beforeEach((to, from, next) => {
  // 处理特定的路径重定向
  if (to.path === '/main') {
    return next('/main/index')
  }
  if (to.path === '/main/projects') {
    return next('/main/projects/implementation')
  }

  // 检查用户权限
  const userStore = useUserStore()
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (userStore.userMessage.userId === '') {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
